package cn.itcast.servlet;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("findAll".equals(method)){
            findAll(req,resp);
        }
        if("add".equals(method)) {
            add(req, resp);
        }
        if("delete".equals(method)) {
            delete(req,resp);
        }
        if("findById".equals(method)) {
            findById(req,resp);
        }
        if("updateByUser".equals(method)) {
            updateByUser(req,resp);
        }
    }

    public void updateByUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        User user = new User();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.updateByUser(user);
        req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        String uid = req.getParameter("uid");
        User user = service.findById(uid);
        req.setAttribute("user",user);
        req.getRequestDispatcher("/pages/update.jsp").forward(req,resp);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        String uid = req.getParameter("uid");
        service.deleteById(uid);
        req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        List<User> list = service.findAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/pages/list.jsp").forward(req,resp);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserServiceImpl();
        User user = new User();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
         System.out.println(user);// 用于调试
        int count = service.insertAdd(user);
        //方法一（若是重定向此方法可能失效（待测试））
       /* if(count > 0) {
            findAll(req,resp);
        }*/
        //方法二
            req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
    }
}
